package rpt.yto.flex.remote.data.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.sf.json.JSONArray;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import rpt.yto.flex.remote.data.bean.RPTNodeBean;
import rpt.yto.flex.remote.data.dao.INodeDAO;

public class NodeBiz {

	private List<Map<String , Object>> results;
	
	private static ApplicationContext context;
	private static INodeDAO nodeDAO;
	
	static {
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		nodeDAO = (INodeDAO) context.getBean("nodeDAO");
	}
	
	public NodeBiz() {
		
	}
	
	public String resultNodeJSON(String id) throws Exception { 
		
		List<RPTNodeBean> nodeList = nodeDAO.queryNodeById(id);
		
//		for(RPTNodeBean node : nodeList) {
//			
//			genNodeList(node.getNodecode() , node);
//			
//		}
		
		if(nodeList != null && nodeList.size() > 0) {
			results = new ArrayList<Map<String , Object>>();
			
			for(RPTNodeBean node : nodeList) {
				results.add(node.getBeanToMap());
			}
			
			return JSONArray.fromObject(results).toString();
			
		}
		
 		return null;
	}
	
	public String modifyNode(String pk_nodes , String nodecode , String nodeid , String nodename , String parentcode , String isparent) throws Exception {
		
		Map<String , String> param = new ConcurrentHashMap<String , String>();
		param.put("pk_nodes", pk_nodes);
		param.put("nodecode", nodecode);
		param.put("nodeid", nodeid);
		param.put("nodename", nodename);
		param.put("parentcode", parentcode);
		param.put("isparent", isparent);
		
		Integer count = nodeDAO.queryNodeByNodeId(param);
		if(count > 0)
			return "0";
		
		return nodeDAO.modifyNode(param);
	}
	
	public String addNode(String nodecode , String nodeid , String nodename , String parentcode , String isparent) throws Exception {
		
		Map<String , String> param = new ConcurrentHashMap<String , String>();
		param.put("nodecode", nodecode);
		param.put("nodeid", nodeid);
		param.put("nodename", nodename);
		param.put("parentcode", parentcode);
		param.put("isparent", isparent);
		
		Integer count = nodeDAO.queryNodeByNodeId(param);
		if(count > 0)
			return "0";
		
		Integer maxNum = Integer.valueOf(String.valueOf(nodeDAO.queryMaxNodePk()));
		String pk_nodes = String.valueOf(++maxNum);
		
		StringBuilder builderPK = new StringBuilder();
		for(int i = 20 - pk_nodes.length() ; i > 0 ; i --) {
			builderPK.append("0");
		}
		
		builderPK.append(pk_nodes);
		
		param.put("pk_nodes", builderPK.toString());
		
		try {
			nodeDAO.addNode(param);
			
			return "1";
		} catch(Exception e ) {
			return "0";
		}
	}
	
	public String deleteNode(String pk_nodes) throws Exception {
		
		if(nextNodeExsits(pk_nodes))
			return "0";
		
		return nodeDAO.deleteNode(pk_nodes);
	}
	
	private boolean nextNodeExsits(String pk_nodes) throws Exception {
		
		if(nodeDAO.nextNodeExsits(pk_nodes))
			return true;
		
		return false;
	}
	
	public String exsitsTemplate(String nodeid) throws Exception {
		
		if(nodeDAO.exsitsTemplate(nodeid) > 0)
			return "true";
		
		return "false";
	}
	
	public String deleteTemplate(String nodeid) throws Exception {
		
		String pk_table = nodeDAO.queryTemplateId(nodeid);
		
		Map<String , String> param = new ConcurrentHashMap<String , String>();
		
		param.put("tablename", "rpt_table_b");
		param.put("pk_table", pk_table);
		
		nodeDAO.deleteTemplate(param);
		
		param.put("tablename", "rpt_table");
		nodeDAO.deleteTemplate(param);
		
		return "true";
		
	}
	
	
	
	
	
	// 误删
//	private void genNodeList(String id , RPTNodeBean parentNode) throws Exception {
//		
//		List<RPTNodeBean> nodeList = nodeDAO.queryNodeById(id);
//		
//		if(nodeList != null && nodeList.size() > 0) {
//			
//			parentNode.setChildren(nodeList);
//			
//			for(RPTNodeBean node : nodeList) {
//				
//				genNodeList(node.getNodecode() , node);
//				
//			}
//			
//		}
//		
//	}
	
}
